<?php
namespace app\common\controller;

use app\behavior;
use think\Controller;
use think\Db;
use think\Request;

class Api extends Controller
{
    private $app_id;
    private $module;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->app_id = config('app_id');

        $this->module = Request::instance()->module();
    }

    /*
     * 根据uid获取token
     */
    protected function getAuthToken($uid)
    {
        $authHandel = new behavior\TokenAuth();
        $token = $authHandel->generateToken($uid,$this->app_id);
        return $token;
    }

    /**
     * 解析token
     * @param $token
     * @return bool
     */

    protected function checkAuthToken($token){
        if(trim($token)){
            $authHandel = new behavior\TokenAuth();
            $token = $authHandel->checkToken($token);
        }else{
            $token = '';
        }
        return $token;
    }

    /**
     * 解析token
     * @param $token $uid
     * @return bool
     */
    protected function checkAdmin($token,$uid){
        if(trim($token)){
            $authHandel = new behavior\TokenAuth();
            $token = $authHandel->checkToken($token);
        }else{
            $token = false;
        }

        $admin = Db::name('admin')->where(['id'=>$uid,'status'=>1])->find();
        if($token && !empty($admin)){
            return true;
        }else{
            return false;
        }
    }
}
